﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using T2.WebSecurity.ASPMVC.Controllers;
using T2.WebSecurity.ASPMVC.Entity;
using Microsoft.QualityTools.Testing.Fakes;
using System.Net.Http;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using System.Collections.Generic;

namespace T2.WebSecurity.UI.MVC.Tests.Controllers
{
    [TestClass]
    public class UserProfileController
    {
        private static List<IUserProfile_ui> retVal;

        [TestMethod]
        public void UserProfile_1_Add()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            UserProfileAPIController controller = new UserProfileAPIController();
            UserProfile_ui param = new UserProfile_ui();
            param.Action = "Add";
            param.Role = "user";
            param.Address = "45";
            param.CellPhone = "88888";
            param.City = "Passe";
            param.FirstName = "Pahan";
            param.IsActive = false;
            param.LastName = "Entata";
            param.OfficePhone = "777777";
            param.PostalCode = "343";
            param.Region = "NE";
            param.Title = "Mr.";
            param.UserName = "pahan";
            

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostUserProfile(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }
        }

        [TestMethod]
        public void UserProfile_2_GetAll()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            UserProfileAPIController controller = new UserProfileAPIController();
            UserProfile_ui param = new UserProfile_ui();
            param.Action = "GetAll";
            param.PageNumber = 1;
            param.PageSize = 100;
            param.Role = "user";
            param.SortColumn = "UserId";
            param.SortType = "asc";

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                retVal = controller.GetUserProfile(param.Action, "trooks", param.SortColumn, param.SortType, param.Role, param.PageNumber, param.PageSize);
                Assert.IsNotNull(retVal);
                Assert.IsTrue(retVal[0].UserId > 0);
            }

        }

        [TestMethod]
        public void UserProfile_3_Edit()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            UserProfileAPIController controller = new UserProfileAPIController();
            UserProfile_ui param = new UserProfile_ui();
            param.Action = "Update";
            param.UserId = 3026;
            param.Role = "user";
            param.Address = "99";
            param.CellPhone = "45454545";
            param.City = "Passe";
            param.FirstName = "Pahan";
            param.IsActive = false;
            param.LastName = "Entata";
            param.OfficePhone = "777777";
            param.PostalCode = "343";
            param.Region = "NE";
            param.Title = "Mr.";
            param.UserName = "pahan";
            param.TS = "0-0-0-0-0-1-17-121";
            param.CreatedBy = "duminda";
            param.CreatedDt = Convert.ToDateTime("2013-08-29 16:04:36.197");
            param.ModifiedBy = "duminda";
            param.ModifiedDt = Convert.ToDateTime("2013-08-29 16:05:27.287");


            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostUserProfile(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }
        }

        [TestMethod]
        public void UserProfile_4_Delete()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            UserProfileAPIController controller = new UserProfileAPIController();
            UserProfile_ui param = new UserProfile_ui();
            param.Action = "Remove";
            param.UserId = 3026;
            param.TS = "0-0-0-0-0-1-17-122";
            param.CreatedBy = "duminda";
            param.CreatedDt = Convert.ToDateTime("2013-08-29 16:04:36.197");
            param.ModifiedBy = "duminda";
            param.ModifiedDt = Convert.ToDateTime("2013-08-29 16:05:27.287");


            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostUserProfile(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }
        }
    }
}
